package org.dreamfly.dreamalarm.service;

import java.util.Timer;
import java.util.TimerTask;

import org.dreamfly.dreamalarm.AlertActivity;
import org.dreamfly.dreamalarm.DreamAlarmActivity;
import org.dreamfly.dreamalarm.util.WeiboUtils;

import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.os.IBinder;
import android.util.Log;

public class WeiboService extends Service {

	private Timer timer;
	private TimerTask getWeiboCommentTask;
	private TimerTask shareToWeiboTask;
	private long weibo_id;
	private static long workTime;
	private static final int WEIBO_TASK_PERIOD = 1000*30;
	public static final int WEIBO_SHARE_DELAY = 1000*60*15;

	public static boolean shutDown=false; //对外提供控制权
	
	public static void changeWorkTime(long workTime){
		if(workTime<WeiboService.workTime){
			WeiboService.workTime=workTime;
		}
	}
	
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		timer = new Timer();
		shutDown=false;
		super.onCreate();
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		if (shareToWeiboTask != null) {
			shareToWeiboTask.cancel();
		}
		if (getWeiboCommentTask != null) {
			getWeiboCommentTask.cancel();
		}
		if (timer != null) {
			timer.cancel();
		}
		shutDown=false;
		
		Editor sharedata = getSharedPreferences(DreamAlarmActivity.SHAREPREFERENCE_DATA, 0)
				.edit();
		sharedata.putBoolean(AlertActivity.weibo_service_key, false);
		sharedata.putLong("weibo_id",0);
		sharedata.commit();
		
		Log.d("weiboservice","service destroy");
		super.onDestroy();
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		workTime = intent.getLongExtra("work_time", 0);
		Log.d("WeiboService.workTime", String.valueOf(workTime));
		shareToWeiboTask = new TimerTask() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				Log.d("WeiboService.test", "live");
				if (workTime-System.currentTimeMillis() < WEIBO_SHARE_DELAY) {
					Log.d("WeiboService.share", "redy to share");
					WeiboUtils.getInstance().shareToWeiBo("一不小心我又睡过了,帮帮我,发表评论喊我起床吧... 今日随机晚起编号："+  (int)(Math.random()*100)+"(by DreamAlarm)",
							WeiboService.this);
					if(shareToWeiboTask != null){
						shareToWeiboTask.cancel();
						weibo_id = getSharedPreferences(
								DreamAlarmActivity.SHAREPREFERENCE_DATA, 0).getLong("weibo_id",
								0);
						if (weibo_id != 0) {
							getWeiboCommentTask = new TimerTask() {
								@Override
								public void run() {
									// TODO Auto-generated method stub
									WeiboUtils.getInstance().getWeiboComment(weibo_id,
											WeiboService.this);
									
									//等待时间过长则关闭服务
									if (shutDown || workTime-System.currentTimeMillis() < -WEIBO_SHARE_DELAY){
										WeiboService.this.stopSelf();
									}
								}

							};
							timer.schedule(getWeiboCommentTask, 0, WEIBO_TASK_PERIOD);
						}
					}
				}
				
				if(shutDown || workTime<System.currentTimeMillis()){
					WeiboService.this.stopSelf();
				}
			}
		};
		if(workTime != 0){
			timer.schedule(shareToWeiboTask, 0, WEIBO_TASK_PERIOD);
		}
		
		return super.onStartCommand(intent, flags, startId);
	}
}
